﻿using StoryPlan.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace StoryPlan.Api
{
    [Authorize]
    public class StoryController : ApiControllerBase
    {
        private MainDataContext db = new MainDataContext();

        // GET api/story
        public IEnumerable<Story> Get()
        {
            var id = GetUserId();

            var stories = db.Stories.Where(s => s.UserId == id);

            return stories.AsEnumerable();
        }

        // GET api/story/5
        public Story Get(int id)
        {
            var uid = GetUserId();
            var story = db.Stories.FirstOrDefault(s => s.UserId == uid && s.Id == id);

            if (story == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);

            return story;
        }

        // POST api/story
        public HttpResponseMessage Post(Story value)
        {
            if (!ModelState.IsValid)
            {
                var error = new HttpError(ModelState, true);
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, error);
            }
            
            var userId = GetUserId();
            if (value.UserId != userId)
                value.UserId = userId;
            value.CreatedDate = DateTime.UtcNow;
            value.LastModifiedDate = value.CreatedDate;

            db.Stories.Add(value);
            db.SaveChanges();

            return Request.CreateResponse(HttpStatusCode.Created);
        }

        // PUT api/story/5
        public HttpResponseMessage Put(int id, Story value)
        {
            var userId = GetUserId();
            var oldItem = db.Stories.FirstOrDefault(s => s.Id == id && s.UserId == userId);
            if (oldItem == null)
                return new HttpResponseMessage(HttpStatusCode.NotFound);

            if (!ModelState.IsValid)
            {
                var error = new HttpError(ModelState, true);
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, error);
            }

            if (value.Id != id)
                return new HttpResponseMessage(HttpStatusCode.BadRequest);

            oldItem.LastModifiedDate = DateTime.UtcNow;
            oldItem.Title = value.Title;
            oldItem.Subtitle = value.Subtitle;
            oldItem.Description = value.Description;

            db.SaveChanges();

            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }

        // DELETE api/story/5
        public HttpResponseMessage Delete(int id)
        {
            var userId = GetUserId();
            var item = db.Stories.FirstOrDefault(s => s.Id == id && s.UserId == userId);
            if (item == null)
                return new HttpResponseMessage(HttpStatusCode.NotFound);

            db.Stories.Remove(item);

            db.SaveChanges();

            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
    }
}
